package com.ruoyi.develop.lvDo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvDo.mapper.LvDoMapper;
import com.ruoyi.develop.lvDo.domain.LvDo;
import com.ruoyi.develop.lvDo.service.ILvDoService;

/**
 * 请购单Service业务层处理
 *
 * @author qh
 * @date 2024-08-09
 */
@Service
public class LvDoServiceImpl implements ILvDoService {
    @Resource
    private LvDoMapper lvDoMapper;
    @Resource
    private GoodsSaledoMapper goodsSaledoMapper;



    /**
     * 查询请购单
     *
     * @param id 请购单主键
     * @return 请购单
     */
    @Override
    public LvDo selectById(String id) {
        return lvDoMapper.selectById(id);
    }

    /**
     * 查询请购单列表
     *
     * @param entity 请购单
     * @return 请购单
     */
    @Override
    public List<LvDo> selectList(LvDo entity) {
        return lvDoMapper.selectList(entity);
    }

    @Override
    public List<LvDo> listHaveBuy(LvDo entity) {
        return lvDoMapper.listHaveBuy(entity);
    }

    /**
     * 新增请购单
     *
     * @param entity 请购单
     * @return 结果
     */
    @Override
    public int insert(LvDo entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isBlank(entity.getNos())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(new Date());
            String codeMaxString = lvDoMapper.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 如果获取的最大编号是 11位数 则补全成12位
                if(codeMaxString.length()==11){
                    codeMaxString = codeMaxString.substring(0,8)+"0"+codeMaxString.substring(8);
                }
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return lvDoMapper.insert(entity);
    }

    /**
     * 修改请购单
     *
     * @param entity 请购单
     * @return 结果
     */
    @Override
    public int update(LvDo entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvDoMapper.update(entity);
    }

    /**
     * 批量删除请购单
     *
     * @param ids 需要删除的请购单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        // 删除请购明细表
        for (String id : ids) {
            GoodsSaledo goodsSaledo = new GoodsSaledo();
            goodsSaledo.setMainId(id);
            List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectList(goodsSaledo);
            for (GoodsSaledo saledo : goodsSaledoList) {
                goodsSaledoMapper.deleteById(saledo.getId());
            }
        }
        return lvDoMapper.deleteByIds(ids);
    }

    /**
     * 删除请购单信息
     *
     * @param id 请购单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvDoMapper.deleteById(id);
    }

    @Override
    public String selectMaxCode(String today) {
        return lvDoMapper.selectMaxCode(today);
    }

    @Override
    public List<LvDo> selectByNos(String today) {
        return lvDoMapper.selectByNos(today);
    }

}
